<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="时间">
        <el-date-picker
          v-model="dateRange"
          :clearable="false"
          :editable="false"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="站点名称" prop="pointName">
        <el-input
          v-model="queryParams.pointName"
          placeholder="请输入站点名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报警类型" prop="paramId">
        <el-select v-model="queryParams.paramId" placeholder="请选择报警类型" >
          <el-option label=" ——— 全部 ——— " value=""></el-option>
          <el-option
            v-for="paramInfo in paramInfoList"
            :key="paramInfo.id"
            :label="paramInfo.name"
            :value="paramInfo.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="paramAlarmRecList" :max-height="tabHt">
      <el-table-column label="报警对象" align="center" prop="pointName" width="250" />
      <el-table-column label="告警时间" align="center" prop="alarmTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.alarmTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="告警类型" align="center" prop="paramName" width="180" />
      <el-table-column label="报警标记" align="center" prop="alarmMark" :formatter="valTypeFormat" width="180"/>
      <el-table-column label="告警内容" align="left" prop="alarmTxt" />
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
  import { listParamAlarmRec } from "@/api/basic/paramAlarmRec";
  import { listParamInfo } from "@/api/basic/paramInfo";

  export default {
    name: "ParamAlarmRec",
    components: {
    },
    data() {
      return {
        valTypeOptions:[],
        paramInfoList:[],
        dateRange: "",
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 参数告警存储表格数据
        paramAlarmRecList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          pointId: null,
          paramId: '',
          alarmGroupId: null,
          deptId: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      };
    },
    computed: {
      tabHt(){
        return this.$store.state.app.tableHeight.ht - 40
      }
    },
    mounted() {
      let nowDate = new Date();
      let befDate = new Date(nowDate.getTime() + 1 * 24 * 3600 * 1000);
      let byear = befDate.getFullYear();
      let bmonth = befDate.getMonth() + 1;
      let bday = befDate.getDate();
      let endTime = `${byear}-${bmonth}-${bday}`;
      // 向前推迟一周的时间点
      befDate = new Date(nowDate.getTime() - 7 * 24 * 3600 * 1000);
      byear = befDate.getFullYear();
      bmonth = befDate.getMonth() + 1;
      bday = befDate.getDate();
      let startTime = `${byear}-${bmonth}-${bday}`;
      this.dateRange = [startTime,endTime];
      this.getList();
    },
    created() {
      this.getDicts("basic_val_type").then(response => {
        this.valTypeOptions = response.data;
      });
      this.getParamInfos();
    },
    methods: {
      valTypeFormat(row) {
        return this.selectDictLabel(this.valTypeOptions, row.alarmMark);
      },
      /** 查询参数信息配置列表 */
      getParamInfos() {
        let obj={
          pageNum: 1,
          pageSize: 100,
        }
        listParamInfo(obj).then(response => {
          this.paramInfoList = response.rows;
        });
      },
      /** 查询参数告警存储列表 */
      getList() {
        this.loading = true;
        listParamAlarmRec(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.paramAlarmRecList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },

      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },


    }
  };
</script>
